package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.base.Account;
import info.openlvb.openface.client.base.person.MemberInGroupWindow;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Label;

/**
 * ajoute un membre dans un groupe
 * @author gbtux
 *
 */

public class addMemberInGroupListener extends ButtonListenerAdapter {

	private String group;
	private String newuser="";
	private MemberInGroupWindow memberInGroupWindow;
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	DialogBox dialog;

	public addMemberInGroupListener(MemberInGroupWindow memberInGroupWindow, String group) {
		this.group = group;
		this.memberInGroupWindow = memberInGroupWindow;
	}

	public void onClick(Button button, EventObject e) {
		ldapService.addUserInGroup(group, newuser, new AsyncCallback<Void>() {
			public void onSuccess(Void theresult) {
				ldapService.searchAccountByUid(newuser, new AsyncCallback<Account>() {
					public void onFailure(Throwable caught) {
						MessageBox.alert("Probleme avec communication vers le backend");
					}
					public void onSuccess(Account result) {
						int row = memberInGroupWindow.getUserTable().getRowCount();
						row++;
						memberInGroupWindow.getUserTable().setWidget(row, 0, new Label(newuser)); //menuFactory(row, group, newuser)
						memberInGroupWindow.getUserTable().setWidget(row, 1,new Label(result.getNom()));
						memberInGroupWindow.getUserTable().setWidget(row, 2,new Label(result.getPrenom()));
						Button button = new Button("Remove");
						button.setIcon("images/cancel.png");
						DeleteUserInGroupListener deleteListener = new DeleteUserInGroupListener(memberInGroupWindow,group,newuser);
						deleteListener.setRow(row);
						button.addListener(deleteListener);
						memberInGroupWindow.getUserTable().setWidget(row, 3,button);
						memberInGroupWindow.getSb().setText("");
					}
					/*public Button menuFactory(int row,String group,String login){
						Button menuButton = new Button();
						menuButton.setText(login);
						menuButton.setIcon("images/configure.png");
						Menu menu = new Menu();
						Item viewItem = new Item("view");
						viewItem.setIconCls("view-icon");
						menu.addItem(viewItem);
						Item deleteItem = new Item("delete this user in this group");
						deleteItem.setIconCls("cancel-icon");
						//deleteItem.addListener(new DeleteUserInGroupListener(memberInGroupWindow,group,login,row));
						menu.addItem(deleteItem);
						menuButton.setMenu(menu);
						return menuButton;
					}*/
				});
			}
			public void onFailure(Throwable caught) {
				MessageBox.alert("Probleme avec communication vers le backend");
			}
		});
	}

	public void setNewuser(String newuser) {
		this.newuser = newuser;
	}
	
}
